<template>
    <div class="about">
        <h1>环信获取token页面</h1>
        <h2>获取的Token：{{Token}}</h2>
    </div>
</template>
<script>
    //import config from '../utils/WebIMConfig';
    //import websdk from 'easemob-websdk';
    import {
        getToken//获取token接口
    } from '../api/im/imapi';
    export default {
        data() {
            return {
                Token : ''
            }
        },
        mounted() {
            //this.WebIMFun()
            this.getToken()
        },
        /*destroyed() {
          this.WebIMFun('close') // 根据自己的实际情况，关闭
        },*/
        methods: {
            /*WebIMFun(type) {
              // 初始化IM SDK
              var WebIM = {}
              WebIM = window.WebIM = websdk
              WebIM.config = config
              //连接初始化，设置各种参数
              WebIM.conn = new WebIM.connection({
                appKey: WebIM.config.appkey,
                isHttpDNS: WebIM.config.isHttpDNS,
                isMultiLoginSessions: WebIM.config.isMultiLoginSessions,
                https: WebIM.config.https,
                url: WebIM.config.socketServer,
                apiUrl: WebIM.config.restServer,
                isAutoLogin: WebIM.config.isAutoLogin,
                autoReconnectNumMax: WebIM.config.autoReconnectNumMax,
                autoReconnectInterval: WebIM.config.autoReconnectInterval,
                delivery: WebIM.config.delivery,
                useOwnUploadFun: WebIM.config.useOwnUploadFun
              })
              if (type == 'close') {//传递参数type如果是close则关闭环信连接，返回false
                WebIM.conn.close()
                return false
              }
              // 链接环信
              WebIM.conn.open({
                user: WebIM.config.username,
                pwd: WebIM.config.password,
                appKey: WebIM.config.appkey
              })
              WebIM.conn.listen({
                onOpened: function() {
                  console.log('成功登录_用户名:' + WebIM.config.username)
                  console.log('成功登录_密码:' + WebIM.config.password)
                  console.log('成功登录_appkey:' + WebIM.config.appkey)
                  // WebIM.conn.setPresence()
                  //WebIM.conn.joinChatRoom({
                  //  roomId: huanxin_room_id // 聊天室Id
                  //}).then(() => {
                  //  console.log('加入聊天室成功')
                  //})
                },
                onClosed: function() {
                  // 处理登出事件
                  console.log('退出成功')
                },
                onPresence: function(message) {
                  console.log(message)
                },
                onTextMessage: function(message) { // 接收消息【从服务器发来的消息】
                  //获取到信息做处理
                  //写自己的逻辑
                  console.log(message)
                },
                onError: function(e) {
                  // 异常捕获
                  console.log('error:' + JSON.stringify(e))
                }
              })
            },*/
            getToken(){
                //获得token
                return getToken().then((res) => {
                    this.Token = res.access_token
                    console.log(res)
                }).catch(err => {
                    console.log(err)
                })
            }
        }

    }
</script>